package com.ccbooks.util.PDB;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import cc.software.books.format.Plugin;
import cc.software.books.format.PluginChm;

import com.ccbooks.util.ContentDisp;
import com.ccbooks.util.TextSizeUtil;
import com.ccbooks.view.BookContentView;

import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class ContentDispPDB implements Runnable{

	//public static String content;
	public String fileName;
	public String fontName;
	public int width;
	public String charSet;
	public int percent;
	public int lineNo;
	public int lineCount;
	public short firstThread;
	
	BooksEnginePDB engine;
	BookContentView bcv;
	private TextView tv;
	private short portraint;
	public int what;	//决定是在哪一个textview显示；
	
	public ContentDispPDB(BookContentView bcv,String fileName, String fontName, int width,
			String charSet,short firstThread,short portraint) {
		super();
		this.bcv = bcv;
		this.fileName = fileName;
		this.fontName = fontName;
		this.width = width;
		this.charSet = charSet;
		this.firstThread = firstThread;
		this.engine = new BooksEnginePDB();
		this.portraint = portraint;
	}
	
	public ContentDispPDB(BookContentView bcv,String fileName, String fontName, int width,
			String charSet,short firstThread,BooksEnginePDB engine,short portraint) {
		super();
		this.bcv = bcv;
		this.fileName = fileName;
		this.fontName = fontName;
		this.width = width;
		this.charSet = charSet;
		this.firstThread = firstThread;
		this.engine = engine;
		this.portraint = portraint;
	}
	
	public void setLine(int lineNo,int lineCount){
		this.lineNo = lineNo;
		this.lineCount = lineCount;
	}
	
	public void firstThread(){	
		try {
	System.out.println("ContentDisp----------------------------------------start---->");		
			
			engine.getPageContent(lineNo, TextSizeUtil.getLineCount(TextSizeUtil.fontSize, portraint));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("ContentDisp----------------------------------------end---->");		
	}
	
	public String secThread(){
		try {
			System.out.println("lineNo"+lineNo);
			System.out.println("lineCount"+lineCount);
			engine.getPageContent(lineNo, TextSizeUtil.getLineCount(TextSizeUtil.fontSize, portraint));
		
		} catch (IndexOutOfBoundsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return BooksEnginePDB. content;
	}
	
	public void nextPage(){
		
	}

	@Override
	public void run() {
		
		while(true){
			if(ContentDisp.startDisp){
				ContentDisp.startDisp = false;
				break;
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		firstThread();
		Handler handler = bcv.bcHandler;
		handler.sendEmptyMessage(what);
		handler.sendEmptyMessage(5);
	}
}
